package gases.as3.librarys.components.ui.text
{
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	import gases.as3.librarys.components.skin.SkinStylesMangaer;
	import gases.as3.librarys.components.skin.data.BaseSkin;
	import gases.as3.librarys.components.skin.data.TextInputSkin;
	import gases.as3.librarys.components.ui.GSkinBox;
	import gases.as3.librarys.components.ui.GUIComponents;
	import gases.as3.util.SkinStyleName;
	
	public class GTextInput extends GUIComponents
	{
		public function GTextInput(txt:String = "", w:Number = 80, h:Number = 20)
		{
			_defuatTxt = txt;
			_width = w;
			_height = h;
			Gwidth = _width;
			Gheight = _height;
			super();
		}
		
		override protected function initialization():void
		{
			super.initialization();
			_background = new GSkinBox();
			_background.setWidth(_width);
			_background.setHeight(_height);
			
			_inputText = new TextField();
			_inputText.text = _defuatTxt;
			_inputText.border = true;
			_inputText.x = 2;
			_inputText.y = 1;
			_inputText.width = _width - 4;
			_inputText.height = _height - 2;
			_inputText.type = TextFieldType.INPUT;
			
			this.addChild(_background);
			this.addChild(_inputText);
			this.restrict = _restrict;
			addMouseListener();
		}
		
		override public function set skin(value:BaseSkin):void
		{
			super.skin = value;
			_inputSkin = value as TextInputSkin;
			_background.source = _inputSkin.background;
		}
		
		public function set text(value:String):void
		{
			_inputText.text = value;
		}
		
		public function get text():String
		{
			return _inputText.text;
		}
		
		public function set color(value:uint):void
		{
			_color = value;
			_inputText.textColor = value;
		}
		
		public function get color():uint
		{
			return _color;
		}
		
		/**
		 * 输入字符数 
		 * @param value
		 * 
		 */		
		public function set maxChar(value:uint):void
		{
			_maxChar = value;
			_inputText.maxChars = value;
		}
		
		/**
		 * 输入字符数 
		 * @param value
		 * 
		 */	
		public function get maxChar():uint
		{
			return _maxChar;
		}
		
		/**
		 * 可输入的字符集
		 * @param value
		 * 
		 */	
		public function set restrict(value:String):void
		{
			_restrict = value;
			_inputText.restrict = value;
		}
		
		/**
		 * 可输入的字符集
		 * @param value
		 * 
		 */	
		public function get restrict():String
		{
			return _restrict;
		}
		
		/**
		 * 是否是密码样式 
		 * @return 
		 * 
		 */	
		public function set isPassword(value:Boolean):void
		{
			_isPassword = value;
			_inputText.displayAsPassword = value;
		}
		
		/**
		 * 是否是密码样式 
		 * @return 
		 * 
		 */	
		public function get isPassword():Boolean
		{
			return _isPassword;
		}
		
		/**
		 * 是否禁选择 
		 * @return 
		 * 
		 */	
		public function set selectEnable(value:Boolean):void
		{
			_selectEnable = value;
			_inputText.selectable = value;
		}
		
		/**
		 * 是否禁选择 
		 * @return 
		 * 
		 */	
		public function get selectEnable():Boolean
		{
			return _selectEnable;
		}
		
		/**
		 * 是否禁用 
		 * @return 
		 * 
		 */	
		public function set enable(value:Boolean):void
		{
			_enable = value;
			_inputText.mouseEnabled = value;
		}
		
		/**
		 * 是否禁用 
		 * @return 
		 * 
		 */		
		public function get enable():Boolean
		{
			return _enable;
		}
		
		/**
		 * 改变皮肤样式 
		 * 
		 */		
		private function chageSkinState():void
		{
		}
		
		private function onFocusIn(e:FocusEvent):void
		{
			_hasFocus = true;
			e.stopImmediatePropagation();
		}
		
		private function onFocusOut(e:FocusEvent):void
		{
			_hasFocus = false;
			e.stopImmediatePropagation();
		}
		
		private function onInputTextChange(e:Event):void
		{
			e.stopImmediatePropagation();
		}
		
		private function onRollOut(e:Event):void
		{
			e.stopImmediatePropagation();
		}
		
		private function onRollOver(e:Event):void
		{
			e.stopImmediatePropagation();
		}
		
		private function addMouseListener():void
		{
			this.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			this.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			_inputText.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
			_inputText.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
			_inputText.addEventListener(Event.CHANGE, onInputTextChange);
		}
		
		private function removeMouseListener():void
		{
			_inputText.removeEventListener(FocusEvent.FOCUS_IN, onFocusIn);
			_inputText.removeEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
			_inputText.removeEventListener(Event.CHANGE, onInputTextChange);
			this.removeEventListener(MouseEvent.ROLL_OUT, onRollOut);
			this.removeEventListener(MouseEvent.ROLL_OVER, onRollOver);
		}
		
		override public function dispose():void
		{
			removeMouseListener();
			if(this.contains(_background))
			{
				this.removeChild(_background);
				this.removeChild(_inputText);
			}
			_background.dispose();
			_background = null;
			_inputText = null;
			_inputSkin = null;
			super.dispose();
		}
		
		
		private var _defuatTxt:String = "";
		private var _maxChar:uint = 0;
		private var _color:uint = 0;
		private var _isPassword:Boolean = false;
		private var _enable:Boolean = true;
		private var _selectEnable:Boolean = true;
		private var _background:GSkinBox;
		private var _inputText:TextField;
		private var _inputSkin:TextInputSkin;
		private var _restrict:String = null;
		private var _width:Number;
		private var _height:Number;
		
		private var _hasFocus:Boolean = false;//是否获得焦点
	}
}



